package com.bootdo.market.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.ProductDO;
import com.bootdo.market.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-12 17:36:32
 */

@Controller
@RequestMapping("/market/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private DuplicateDao duplicateDao;

    @GetMapping()
    @RequiresPermissions("market:product:product")
    String Product() {
        return "market/product/product";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:product:product")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ProductDO> productList = productService.list(query);
        int total = productService.count(query);
        PageUtils pageUtils = new PageUtils(productList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:product:add")
    String add() {
        return "market/product/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:product:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ProductDO product = productService.get(id);
        model.addAttribute("product", product);
        return "market/product/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:product:add")
    public R save(ProductDO product) {
        if (productService.save(product) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:product:edit")
    public R update(ProductDO product) {
        productService.update(product);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:product:remove")
    public R remove(Integer id) {
        if (productService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:product:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        productService.batchRemove(ids);
        return R.ok();
    }

    @RequestMapping(value = "/productUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productUniqueAdd(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/productUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R productUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.productUniqueEdit(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
